package com.xayy.health.ds.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.kawins.base.result.ResultBody;
import cn.kawins.base.utils.CollUtil;
import com.xayy.health.mgt.model.HealthAlgorithmManage;
import com.xtck.ha.common.core.constant.HttpStatus;
import com.xtck.ha.common.core.utils.poi.ExcelUtil;
import com.xtck.ha.common.core.web.controller.BaseController;
import com.xtck.ha.common.core.web.domain.AjaxResult;
import com.xtck.ha.common.core.web.page.TableDataInfo;
import com.xayy.health.ds.model.DsHealthAlgorithmManage;
import com.xayy.health.ds.service.DsHealthAlgorithmManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 模型列表 controller
 *
 * @ClassName: DsHealthAlgorithmManageController
 * @Description: 外部访问接口
 * @author liuyuan
 * @date 2025-07-07
 */
@Api(tags = "算法管理表管理")
@RestController
@RequestMapping("/health-mgr-back/v1.0/algorithm")
public class DsHealthAlgorithmManageController extends BaseController {

    @Autowired
    private DsHealthAlgorithmManageService dsHealthAlgorithmManageService;


    @ApiOperation("分页查询算法管理表列表")
    @GetMapping("/getInfo")
    public AjaxResult page(DsHealthAlgorithmManage dsHealthAlgorithmManage)
    {
        startPage();
        List<DsHealthAlgorithmManage> list = dsHealthAlgorithmManageService.selectTList(dsHealthAlgorithmManage);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setList(list);
        rspData.setMsg("查询成功");
        rspData.setTotal(dsHealthAlgorithmManageService.count(dsHealthAlgorithmManage));
        return AjaxResult.success(rspData);
    }

    @ApiOperation("列表查询算法管理表")
    @GetMapping("/list")
    public List<DsHealthAlgorithmManage> list(DsHealthAlgorithmManage dsHealthAlgorithmManage)
    {
        List<DsHealthAlgorithmManage> list = dsHealthAlgorithmManageService.selectTList(dsHealthAlgorithmManage);
        return list;
    }

    @ApiOperation("导出算法管理表列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, DsHealthAlgorithmManage dsHealthAlgorithmManage)
    {
        List<DsHealthAlgorithmManage> list = dsHealthAlgorithmManageService.selectTList(dsHealthAlgorithmManage);
        ExcelUtil<DsHealthAlgorithmManage> util = new ExcelUtil<>(DsHealthAlgorithmManage.class);
        util.exportExcel(response, list, "样例数据");
    }

    @ApiOperation("获取$算法管理表详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dsHealthAlgorithmManageService.selectTById(id));
    }

//    @ApiOperation("新增$算法管理表")
//    @PostMapping("/add")
//    public AjaxResult add(@RequestBody DsHealthAlgorithmManage dsHealthAlgorithmManage)
//    {
//        return toAjax(dsHealthAlgorithmManageService.insertT(dsHealthAlgorithmManage));
//    }

    /**
     * 新建算法信息
     * @param healthAlgorithmManage 算法实体
     * @param algorithmFile 算法文件
     * @return ResultBody
     */
    @PostMapping("save")
    @ApiOperation(value = "新建算法信息")
    public ResultBody saveAlgorithmInfo(DsHealthAlgorithmManage healthAlgorithmManage,
                                        MultipartFile algorithmFile) {
        return dsHealthAlgorithmManageService.saveAlgorithmInfo(healthAlgorithmManage, algorithmFile).body();
    }

    /**
     * 修改算法信息
     * @param dsHealthAlgorithmManage
     * @return 新建算法信息
     */
    @PostMapping("update")
    public ResultBody update(@RequestBody DsHealthAlgorithmManage dsHealthAlgorithmManage)
    {
        DsHealthAlgorithmManage healthAlgorithmManage1 =
                dsHealthAlgorithmManageService.selectTById(dsHealthAlgorithmManage.getId());
        if (BeanUtil.isEmpty(healthAlgorithmManage1)) {
            return ResultBody.failed("当前算法不存在,无法修改!");
        }
        List<DsHealthAlgorithmManage> selectName =
                dsHealthAlgorithmManageService.selectTList(DsHealthAlgorithmManage.builder().algorithmName(dsHealthAlgorithmManage.getAlgorithmName()).build());
        if (CollUtil.isNotEmpty(selectName)) {
            return ResultBody.failed("算法名称:" + dsHealthAlgorithmManage.getAlgorithmName() + "已存在,不可重名");
        }
        DsHealthAlgorithmManage build = DsHealthAlgorithmManage.builder().id(dsHealthAlgorithmManage.getId())
                .algorithmState(dsHealthAlgorithmManage.getAlgorithmState())
                .algorithmDesc(dsHealthAlgorithmManage.getAlgorithmDesc()).build();
//        if (healthAlgorithmManage1.getAlgorithmType() == 5){
//
//        }
        build.setAlgorithmCode(dsHealthAlgorithmManage.getAlgorithmCode());
        build.setAlgorithmPath(dsHealthAlgorithmManage.getAlgorithmPath());
        build.setAlgorithmFileName(dsHealthAlgorithmManage.getAlgorithmFileName());
        build.setServerState(false);
        build.setAlgorithmName(dsHealthAlgorithmManage.getAlgorithmName());
        build.setAlgorithmType(dsHealthAlgorithmManage.getAlgorithmType());
        build.setStartServiceFile(dsHealthAlgorithmManage.getStartServiceFile());
        build.setIsBuiltInAlgorithms(dsHealthAlgorithmManage.getIsBuiltInAlgorithms());
        build.setTriggerType(dsHealthAlgorithmManage.getTriggerType());
        build.setTrialAddr(dsHealthAlgorithmManage.getTrialAddr());
        build.setPredictAddr(dsHealthAlgorithmManage.getPredictAddr());
        /**关闭旧算法的进程*/
        if (healthAlgorithmManage1.getServerState() == true)
            dsHealthAlgorithmManageService.stopAlgorithmServer(build);
//        algorithmParamService.updateListByIdSelective(algorithmModelVo.getAlgorithmParams());
        dsHealthAlgorithmManageService.updateT(build);
        return ResultBody.success("算法状态修改成功");
    }

    @ApiOperation("删除算法管理表")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dsHealthAlgorithmManageService.deleteTByIds(ids));
    }

    /**
     * 启动算法服务
     * @param healthAlgorithmManage 算法实体
     * @return ResultBody
     */
    @PostMapping("startServer")
    @ApiOperation(value = "启动算法服务")
    public ResultBody startAlgorithmServer(@RequestBody DsHealthAlgorithmManage healthAlgorithmManage) {
        return dsHealthAlgorithmManageService.startAlgorithmServer(healthAlgorithmManage).body();
    }

    /**
     * 停止算法服务
     * @param healthAlgorithmManage 算法实体
     * @return ResultBody
     */
    @PostMapping("stopServer")
    @ApiOperation(value = "停止算法服务")
    public ResultBody stopAlgorithmServer(@RequestBody DsHealthAlgorithmManage healthAlgorithmManage) {
        return dsHealthAlgorithmManageService.stopAlgorithmServer(healthAlgorithmManage).body();
    }

    /**
     * 根据类型获取可用算法列表
     * @param type 类型
     * @return ResultBody
     */
    @GetMapping("/getAlgorithmByType/{type}")
    @ApiOperation(value = "根据类型获取可用算法列表", notes = "1:剔野,2:填充,3:平滑,4:事后诊断,5:趋势预测")
    public ResultBody getAlgorithmByType(@PathVariable @ApiParam("1:剔野,2:填充,3:平滑,4:事后诊断,5:趋势预测") Integer type) {
        List<DsHealthAlgorithmManage> algorithmByType = dsHealthAlgorithmManageService.getAlgorithmByType(type);
        return ResultBody.success(algorithmByType);
    }



}
